<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="地市" prop="city">
        <el-input v-model="formData.city" placeholder="请输入地市" />
      </el-form-item>
      <el-form-item label="供电单位" prop="org">
        <el-input v-model="formData.org" placeholder="请输入供电单位" />
      </el-form-item>
      <el-form-item label="年月" prop="yf">
        <el-input v-model="formData.yf" placeholder="请输入年月" />
      </el-form-item>
      <el-form-item label="用户编号" prop="consNo">
        <el-input v-model="formData.consNo" placeholder="请输入用户编号" />
      </el-form-item>
      <el-form-item label="用电地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入用电地址" />
      </el-form-item>
      <el-form-item label="行业类别" prop="industryType">
        <el-select v-model="formData.industryType" placeholder="请选择行业类别">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="市场化属性" prop="marketType">
        <el-select v-model="formData.marketType" placeholder="请选择市场化属性">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="用电类别" prop="elecType">
        <el-select v-model="formData.elecType" placeholder="请选择用电类别">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="供电电压" prop="voltage">
        <el-input v-model="formData.voltage" placeholder="请输入供电电压" />
      </el-form-item>
      <el-form-item label="电量" prop="dianliang">
        <el-input v-model="formData.dianliang" placeholder="请输入电量" />
      </el-form-item>
      <el-form-item label="尖峰电量" prop="jianfeng">
        <el-input v-model="formData.jianfeng" placeholder="请输入尖峰电量" />
      </el-form-item>
      <el-form-item label="峰电量" prop="feng">
        <el-input v-model="formData.feng" placeholder="请输入峰电量" />
      </el-form-item>
      <el-form-item label="平电量" prop="ping">
        <el-input v-model="formData.ping" placeholder="请输入平电量" />
      </el-form-item>
      <el-form-item label="谷电量" prop="gu">
        <el-input v-model="formData.gu" placeholder="请输入谷电量" />
      </el-form-item>
      <el-form-item label="总电费" prop="fee">
        <el-input v-model="formData.fee" placeholder="请输入总电费" />
      </el-form-item>
      <el-form-item label="目录电费" prop="catalogFee">
        <el-input v-model="formData.catalogFee" placeholder="请输入目录电费" />
      </el-form-item>
      <el-form-item label="代征电费" prop="subFee">
        <el-input v-model="formData.subFee" placeholder="请输入代征电费" />
      </el-form-item>
      <el-form-item label="基本电费" prop="basicFee">
        <el-input v-model="formData.basicFee" placeholder="请输入基本电费" />
      </el-form-item>
      <el-form-item label="力调电费" prop="litiaoFee">
        <el-input v-model="formData.litiaoFee" placeholder="请输入力调电费" />
      </el-form-item>
      <el-form-item label="输配电费" prop="spFee">
        <el-input v-model="formData.spFee" placeholder="请输入输配电费" />
      </el-form-item>
      <el-form-item label="线损费用" prop="xiansunFee">
        <el-input v-model="formData.xiansunFee" placeholder="请输入线损费用" />
      </el-form-item>
      <el-form-item label="系统费" prop="systemFee">
        <el-input v-model="formData.systemFee" placeholder="请输入系统费" />
      </el-form-item>
      <el-form-item label="运行容量" prop="rongliang">
        <el-input v-model="formData.rongliang" placeholder="请输入运行容量" />
      </el-form-item>
      <el-form-item label="用户名称" prop="consName">
        <el-input v-model="formData.consName" placeholder="请输入用户名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ElecDataApi, ElecData } from '@/api/knowledge/elecdata'

/** 用电信息 表单 */
defineOptions({ name: 'ElecDataForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  city: undefined,
  org: undefined,
  yf: undefined,
  consNo: undefined,
  address: undefined,
  industryType: undefined,
  marketType: undefined,
  elecType: undefined,
  voltage: undefined,
  dianliang: undefined,
  jianfeng: undefined,
  feng: undefined,
  ping: undefined,
  gu: undefined,
  fee: undefined,
  catalogFee: undefined,
  subFee: undefined,
  basicFee: undefined,
  litiaoFee: undefined,
  spFee: undefined,
  xiansunFee: undefined,
  systemFee: undefined,
  rongliang: undefined,
  consName: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ElecDataApi.getElecData(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ElecData
    if (formType.value === 'create') {
      await ElecDataApi.createElecData(data)
      message.success(t('common.createSuccess'))
    } else {
      await ElecDataApi.updateElecData(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    city: undefined,
    org: undefined,
    yf: undefined,
    consNo: undefined,
    address: undefined,
    industryType: undefined,
    marketType: undefined,
    elecType: undefined,
    voltage: undefined,
    dianliang: undefined,
    jianfeng: undefined,
    feng: undefined,
    ping: undefined,
    gu: undefined,
    fee: undefined,
    catalogFee: undefined,
    subFee: undefined,
    basicFee: undefined,
    litiaoFee: undefined,
    spFee: undefined,
    xiansunFee: undefined,
    systemFee: undefined,
    rongliang: undefined,
    consName: undefined,
  }
  formRef.value?.resetFields()
}
</script>